package com.jhgsys.internal.accident.service.impl;

import com.baomidou.mybatisplus.core.toolkit.StringUtils;
import com.jhgsys.internal.common.entity.FebsConstant;
import com.jhgsys.internal.hazard.entity.AccidentReportSurveyUser;
import com.jhgsys.internal.hazard.mapper.AccidentReportSurveyUserMapper;
import com.jhgsys.internal.hazard.service.IAccidentReportSurveyUserService;
import com.jhgsys.internal.common.entity.QueryRequest;
import com.jhgsys.internal.common.utils.FebsUtil;
import com.jhgsys.internal.system.entity.User;
import org.apache.shiro.SecurityUtils;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.transaction.annotation.Propagation;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.baomidou.mybatisplus.core.toolkit.StringPool;

import java.util.Arrays;
import java.util.Date;
import java.util.List;

/**
 *  Service实现
 *
 * @author zhangwei
 * @date 2024-09-13 10:11:20
 */
@Service
@Transactional(propagation = Propagation.SUPPORTS, readOnly = true, rollbackFor = Exception.class)
public class AccidentReportSurveyUserServiceImpl extends ServiceImpl<AccidentReportSurveyUserMapper, AccidentReportSurveyUser> implements IAccidentReportSurveyUserService {

    /**
     * 查询（分页）
     * @author zhangwei
     * @date 2024-09-13 10:11:20
     * @param request QueryRequest
     * @param accidentReportSurveyUser accidentReportSurveyUser
     * @return IPage<AccidentReportSurveyUser>
    */
    @Override
    public IPage<AccidentReportSurveyUser> findAccidentReportSurveyUserPage(QueryRequest request, AccidentReportSurveyUser accidentReportSurveyUser) {
        LambdaQueryWrapper<AccidentReportSurveyUser> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(AccidentReportSurveyUser::getDeletedFlg, 0);
        // TODO 设置查询条件

        Page<AccidentReportSurveyUser> page = new Page<>(request.getPageNum(), request.getPageSize());
        FebsUtil.pageWrapper(page, request);
        return this.page(page, queryWrapper);
    }

    /**
     * 查询（所有）
     * @author zhangwei
     * @date 2024-09-13 10:11:20
     * @param accidentReportSurveyUser accidentReportSurveyUser
     * @return List<AccidentReportSurveyUser>
     */
    @Override
    public List<AccidentReportSurveyUser> findAccidentReportSurveyUserList(AccidentReportSurveyUser accidentReportSurveyUser) {
	    LambdaQueryWrapper<AccidentReportSurveyUser> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(AccidentReportSurveyUser::getDeletedFlg, 0);
		// TODO 设置查询条件
        if(StringUtils.isNotBlank(accidentReportSurveyUser.getReportId())){
            queryWrapper.eq(AccidentReportSurveyUser::getReportId,accidentReportSurveyUser.getReportId());
        }
		return this.baseMapper.selectList(queryWrapper);
    }

    /**
    * 根据ID查询
    * @author zhangwei
    * @date 2024-09-13 10:11:20
    * @param id
    * @return AccidentReportSurveyUser
    */
    @Override
    public AccidentReportSurveyUser findAccidentReportSurveyUserById(String id){
        return this.baseMapper.selectById(id);
    }

    /**
     * 新增
     * @author zhangwei
     * @date 2024-09-13 10:11:20
     * @param accidentReportSurveyUser accidentReportSurveyUser
     */
    @Override
    @Transactional
    public boolean createAccidentReportSurveyUser(AccidentReportSurveyUser accidentReportSurveyUser) {
        return this.save(accidentReportSurveyUser);
    }

    /**
     * 修改
     * @author zhangwei
     * @date 2024-09-13 10:11:20
     * @param accidentReportSurveyUser accidentReportSurveyUser
     */
    @Override
    @Transactional
    public boolean updateAccidentReportSurveyUser(AccidentReportSurveyUser accidentReportSurveyUser) {
        return this.updateById(accidentReportSurveyUser);
    }

    /**
     * 逻辑删除
     * @author zhangwei
     * @date 2024-09-13 10:11:20
     * @param accidentReportSurveyUser accidentReportSurveyUser
     * @param accidentReportSurveyUserIds accidentReportSurveyUserIds
     */
    @Override
    @Transactional
    public boolean deleteAccidentReportSurveyUser(AccidentReportSurveyUser accidentReportSurveyUser,String accidentReportSurveyUserIds) {
        boolean result = false;
        User user = (User) SecurityUtils.getSubject().getPrincipal();
        List<String> list = Arrays.asList(accidentReportSurveyUserIds.split(StringPool.COMMA));

        accidentReportSurveyUser.setDeletedBy(user.getUserName());
        accidentReportSurveyUser.setDeletedTime(new Date());
        accidentReportSurveyUser.setDeletedFlg(FebsConstant.INVALID);
        int update = this.baseMapper.update(accidentReportSurveyUser,new LambdaQueryWrapper<AccidentReportSurveyUser>().in(AccidentReportSurveyUser::getReportSurveyUserId,list));
        if (update > 0 ){
            result = true;
        }
        return result;
	}

    /**
    * 物理删除
    * @author zhangwei
    * @date 2024-09-13 10:11:20
    * @param accidentReportSurveyUserIds accidentReportSurveyUserIds
    */
    @Override
    @Transactional
    public boolean physicallyDeleteAccidentReportSurveyUser(String accidentReportSurveyUserIds) {
        List<String> list = Arrays.asList(accidentReportSurveyUserIds.split(StringPool.COMMA));
        LambdaQueryWrapper<AccidentReportSurveyUser> wapper = new LambdaQueryWrapper<>();
        wapper.in(AccidentReportSurveyUser::getReportSurveyUserId,list);
        return this.remove(wapper);
    }
}
